<?php
ArrowController::importModel("org.arrowplatform.controls.presentation.table.columns.ArrowAbstractTableColumn");
ArrowController::importModel("org.arrowplatform.controls.common.actions.ArrowAbstractUIAction");
class ArrowTableEditableColumn extends ArrowAbstractTableColumn{

	private $width;
	private $height;
	
	private $editType;
	private $editField;
	private $model;
	
	public function configure(){
		parent::configure();
		$this->addProperties(array(
			"editType" => "text",
			"targetField" => false,
			"titleOn" => false,
			"titleOff" => false,
			"refresh" => false,
			"height" => false,
			"keyColumn" => false,			// jeśli inne niż false to jest to nazwa pola pod jakim przechowywany jest klucz
			"model" => false
		));
	}
	
	public function init(){
		parent::init();
		$this->registerJs( get_class($this), dirname(__FILE__)."/resources/".get_class($this).".js", ".".self::CTRL_NAMESPACE."-table-cell-edit", array( 'refresh' => $this->getProperty('refresh') ) );
		if($this->getProperty("width") != false)
			$this->width = $this->getProperty("width");
		else
			$this->width = 200;
		
		if($this->getProperty("height") != false)
			$this->height = $this->getProperty("height");
		else
			$this->height = 50;
		
		$this->editType = $this->getProperty("editType");
		
		$this->editField = $this->getProperty( "targetField" );
		if( $this->editField === false ) {
			if($this->editType == "select")
				$this->editField = $this->getFirstChild("ArrowFormSelect")->getProperty("name");
			else $this->editField = $this->getProperty("field");
		}
		/*if(strpos($this->field, ":")){
			$tmp = explode(":", $this->field);
			$this->field = array_pop($tmp);
		}*/
		$this->template = $this->getFirstChild("ArrowCtrlTemplate");
			
		if(empty($this->class)) $this->class = 'class=" "';
		
		$this->class = rtrim($this->class, "\"")." ".self::CTRL_NAMESPACE.'-table-cell-edit '.self::CTRL_NAMESPACE.'-table-cell-edit-'.$this->editType.
		 ' '.self::CTRL_NAMESPACE.'-table-cell-edit-'.str_replace( ":", "-",$this->getProperty("field"))."\""; 
		
	}

	public function generateRowCell( $context ){
		
		
		if($this->width == -1) return "";
		
		$str = '<td style="width: '.($this->width).'px;" '.$this->class.' >'.$this->getImage($context);
		$value = $context->getValue($this->field);
		$text = $value;
		
		//$text = ($this->mapping&&isset($this->mapping[$value]))?$this->mapping[$value]:$value;
		//if($text == "") $text = "---";
		
		if(isset($this->mapping[$value]))
			$text = $this->mapping[$value];
		else if(!empty($this->template))
			$text = $this->template->processTemplate($context);
		
		$model = $this->getProperty('model') ;
		if( $model === false ) $model = $context->getModel() ; 
		
		$key = $this->getProperty("keyColumn") ;
		if( $key == false ) $key =  $context->getPKey();
		else $key = $context[$key];
		
		
		$field = $this->editField;
		if(strpos($this->editField, ":") !== false){
			$tmp = explode(":", $this->editField);
			$field = $tmp[1];
			$class = $tmp[0];
			$rel = $context->getRel($class);
			if(isset($rel[0])){
				$model = $rel[0]->getModel();
				$key = $rel[0]->getPKey();
			}else{
				//self relation
				//$model = $context->getModel();
			}
		}
		
		
		switch ($this->editType){
			case "text":
				$str.= '<div class="ctrl-table-cell-edit-presenter">'.$text.'</div>';
				$str.= '<input type="text" class="ctrl-table-cell-edit-value" value="'.$value.'" style="width: '.$this->width.'px;" model="'.$model.'" key="'.$key.'" field="'.$field.'" />';
			break;
			
			case "textarea":
				$str.= '<div class="ctrl-table-cell-edit-presenter">'.$text.'</div>';
				$str.= '<textarea class="ctrl-table-cell-edit-value" style="width: '.$this->width.'px; height: '.$this->height.'px;" model="'.$model.'" key="'.$key.'" field="'.$field.'" >'.$value.'</textarea>';
			break;
			
			case "select":
				$str.= '<div class="ctrl-table-cell-edit-presenter">'.$text.'</div>';
				$str.= '<div class="ctrl-table-cell-edit-value-cont" model="'.$model.'" key="'.$key.'" field="'.$field.'" >';
				$select = $this->getFirstChild("ArrowFormSelect");
				$select->setProperty("selected", $value);
				$select->setExternalProperty( "style", "width: {$this->width}px; float: left;");
				$str.= $select->generate();
				$str.= '</div>';				
			break;
			case "boolswitch":
				//$titlebool = ($value)?$this->getProperty("titleOn"): $this->getProperty("titleOff") ;
				//$titlebool = ($titlebool == false)? "" : "title=\"$titlebool\"" ;
				$currentClass = ($value)?'ctrl-table-cell-edit-on':'ctrl-table-cell-edit-off';
				$str.= '<div class="ctrl-table-cell-edit-switch '.$currentClass.'" model="'.$model.'" key="'.$key.'" field="'.$field.'"></div>';
			break;
		}
		
		$str.= '</td>';
		return $str;
	}
	

}

?>